package rm.metrology.server;

import java.util.ArrayList;
import java.util.List;

import rm.metrology.shared.UncertaintyEntity;

public class UncertaintyDAO extends BaseDAO {

	
	private static final UncertaintyDAO fInstance = new UncertaintyDAO();
	
	public static final UncertaintyDAO getInstance() {
		return fInstance;
	}
	

	private boolean isCacheDirty = true;
	
	private List<UncertaintyEntity> fUncertainties = new ArrayList<UncertaintyEntity>();
	
	private UncertaintyDAO() {
	}
	
	public List<UncertaintyEntity> insert(UncertaintyEntity e) {
		validate(e);
		ofy().put(e);
		emailStaff("Uncertainty Added", "<p/>Uncertainty Details :<p/>" + e.toString());
		isCacheDirty = true;
		return selectAll();
	}
	
	public List<UncertaintyEntity> delete(Long id) {
		UncertaintyEntity e = ofy().get(UncertaintyEntity.class, id);
		ofy().delete(e);
		emailStaff("Uncertainty Removed", "<p/>Uncertainty Details :<p/>" + e.toString());
		isCacheDirty = true;
		return selectAll();
	}	
	
	public List<UncertaintyEntity> selectAll() {
		if (isCacheDirty) {
			fUncertainties.clear();
			fUncertainties.addAll(ofy().query(UncertaintyEntity.class).order("fCalibType").order("fRangeStart").list());
			isCacheDirty = false;
		}
		return fUncertainties;
	}
	
}
